package com.example.star.controller;

import com.example.item.Star;
import com.example.smartcommon.util.R;
import com.example.star.service.StarService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/star/")
@Tag(name = "显示收藏数据")
public class StarController {
    @Autowired
    private StarService starService;

    @Operation(summary = "获得所有收藏数据")
    @GetMapping(value = "/getAllStar")
    public R getAllStar() {
        List<Star> starList = starService.list();
        return R.ok("查询所有收藏数据成功").data("starList", starList);
    }
    
    @Operation(summary = "增加收藏")
    @PostMapping(value = "/addStar")
    public R addStar(@RequestBody Star star) {
        boolean flag = starService.save(star);
        if (flag) {
            return R.ok("增加收藏成功");
        } else {
            return R.error("增加收藏失败");
        }
    }

    @Operation(summary = "取消收藏")
    @GetMapping(value = "/deleteStar/{id}")
    public R deleteStar(@PathVariable("id") Integer starId) {
        boolean flag = starService.removeById(starId);
        if (flag) {
            return R.ok("取消收藏成功");
        } else {
            return R.error("取消收藏失败");
        }
    }

    @Operation(summary = "按照收藏编号查询数据")
    @GetMapping(value = "/getStarById/{id}")
    public R getByIdStar(@PathVariable("id") Integer starId) {
        Star star = starService.getById(starId);
        return R.ok("按照收藏编号查询数据成功").data("star", star);
    }

    @Operation(summary = "修改收藏数据")
    @PostMapping(value = "/updateStar")
    public R updateStar(@RequestBody Star star) {
        boolean flag =  starService.updateById(star);
        if (flag) {
            return R.ok("修改收藏成功");
        } else {
            return R.error("修改收藏失败");
        }
    }
}
